package com.sd.repay.task;

import com.sd.repay.pojo.RepayPlan;
import com.sd.repay.service.RepayPlanService;
import com.sd.repay.utils.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 终态还款计划清算任务
 * Created by sd on 2018/08/28.
 */
@Component
public class RepayPlanSettleTask {
    private static Logger log = LoggerFactory.getLogger(RepayPlanSettleTask.class);

    @Autowired
    private RepayPlanService repayPlanService;

    /**
     * 终态的还款计划进行资金清算
     */
    public void executeRepayPlanSettle() {
        log.info("--------清算终态还款计划任务开始执行---------");
        try {
            List<RepayPlan> completePlans = repayPlanService.getAllCompletePlan();
            if (CollectionUtils.isEmpty(completePlans)) {
                log.info("没有需要清算的还款计划");
                return;
            }
            for (RepayPlan plan : completePlans) {
                try {
                    repayPlanService.settlePlanInfo(plan);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("还款计划批次号[{}]进行清算异常[{}]", new Object[]{plan.getBatchNo(), e});
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("-----------清算终态还款计划任务异常[{}]--------------", e);
        }
        log.info("-----------清算终态还款计划任务完成--------------");
        WebUtil.removeBlockTaskBySpringId(WebUtil.getClassSimpleName(this.getClass()));

    }

}
